package com.sskj.gocoin.utils;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.sskj.gocoin.R;
import com.sskj.gocoin.common.App;

/**
 * <pre>
 *     author : 吕志豪
 *     e-mail : 1030753080@qq.com
 *     time   : 2018/04/10
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class TipUtil {
    public static MaterialDialog getTip(Context context, String content, OnTipSure onTipSure) {
        return new MaterialDialog.Builder(context)
                .title(App.INSTANCE.getString(R.string.strTipUtil200))
                .content(content)
                .positiveText(App.INSTANCE.getString(R.string.strTipUtil201))
                .negativeText(App.INSTANCE.getString(R.string.strTipUtil202))
//                .negativeText("")
                .onAny((dialog, which) -> {
                    if (which == DialogAction.POSITIVE) {
                        dialog.dismiss();
                        onTipSure.onSure();
                    } else if (which == DialogAction.NEGATIVE) {
                        dialog.dismiss();
                    }
                }).build();
    }

    public static AlertDialog getTip(Context context, String content, String rightText, OnTipSure onTipSure) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.dialog_fabi_tip, null);
        ((TextView) view.findViewById(R.id.tv_desc)).setText(content);
        ((TextView) view.findViewById(R.id.tv_apply)).setText(rightText);
        builder.setView(view);
        builder.setCancelable(true);
        AlertDialog tipDialog = builder.create();
        ClickUtil.click(view.findViewById(R.id.tv_cancel), () -> {
            tipDialog.dismiss();
        });
        ClickUtil.click(view.findViewById(R.id.tv_apply), () -> {
            tipDialog.dismiss();
            onTipSure.onSure();
        });
        return tipDialog;
    }

    public static AlertDialog getTip(Context context, String title, String content, String rightText, OnTipSure onTipSure) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.dialog_fabi_tip, null);
        ((TextView) view.findViewById(R.id.tv_desc)).setText(content);
        ((TextView) view.findViewById(R.id.tv_desc)).setTextSize(20);
        ((TextView) view.findViewById(R.id.tv_apply)).setText(rightText);
        ((TextView) view.findViewById(R.id.tv_title)).setText(title);
        builder.setView(view);
        builder.setCancelable(true);
        AlertDialog tipDialog = builder.create();
        ClickUtil.click(view.findViewById(R.id.tv_cancel), () -> {
            tipDialog.dismiss();
        });
        ClickUtil.click(view.findViewById(R.id.tv_apply), () -> {
            tipDialog.dismiss();
            onTipSure.onSure();
        });
        return tipDialog;
    }

    /**
     * @param context
     * @param type      00 ->买入市价，01->买入限价 ，10->卖出市价， 11->卖出限价
     * @param price
     * @param stopwin
     * @param stoploss
     * @param num
     * @param onTipSure
     * @return
     */
    public static AlertDialog getTip(Context context, int type, String price, String stopwin, String stoploss, String num, OnTipSure onTipSure) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.dialog_create_order, null);
        switch (type) {
            case 0://买入市价
                ((TextView) view.findViewById(R.id.orientation_value)).setText(App.INSTANCE.getString(R.string.strNewTradePositionFragment209));
                ((TextView) view.findViewById(R.id.orientation_value)).setTextColor(ContextCompat.getColor(context, R.color.market_up));
                ((TextView) view.findViewById(R.id.type_value)).setText(App.INSTANCE.getString(R.string.strTipUtil204));
                ((TextView) view.findViewById(R.id.price_value)).setText(price);
                break;
            case 1://买入限价
                ((TextView) view.findViewById(R.id.orientation_value)).setText(App.INSTANCE.getString(R.string.strNewTradePositionFragment209));
                ((TextView) view.findViewById(R.id.orientation_value)).setTextColor(ContextCompat.getColor(context, R.color.market_up));
                ((TextView) view.findViewById(R.id.type_value)).setText(App.INSTANCE.getString(R.string.strTipUtil206));
                ((TextView) view.findViewById(R.id.price_value)).setText(price);
                break;
            case 2://卖出市价
                ((TextView) view.findViewById(R.id.orientation_value)).setText(App.INSTANCE.getString(R.string.strNewTradePositionFragment210));
                ((TextView) view.findViewById(R.id.orientation_value)).setTextColor(ContextCompat.getColor(context, R.color.market_down));
                ((TextView) view.findViewById(R.id.type_value)).setText(App.INSTANCE.getString(R.string.strTipUtil204));
                ((TextView) view.findViewById(R.id.price_value)).setText(price);
                break;
            case 3://卖出限价
                ((TextView) view.findViewById(R.id.orientation_value)).setText(App.INSTANCE.getString(R.string.strNewTradePositionFragment210));
                ((TextView) view.findViewById(R.id.orientation_value)).setTextColor(ContextCompat.getColor(context, R.color.market_down));
                ((TextView) view.findViewById(R.id.type_value)).setText(App.INSTANCE.getString(R.string.strTipUtil206));
                ((TextView) view.findViewById(R.id.price_value)).setText(price);
                break;
        }
        ((TextView) view.findViewById(R.id.stopwin_value)).setText(stopwin);
        ((TextView) view.findViewById(R.id.stoploss_value)).setText(stoploss);
        ((TextView) view.findViewById(R.id.count_value)).setText(num);
        builder.setView(view);
        builder.setCancelable(true);
        AlertDialog tipDialog = builder.create();
        ClickUtil.click(view.findViewById(R.id.tv_cancel), () -> {
            tipDialog.dismiss();
        });
        ClickUtil.click(view.findViewById(R.id.tv_apply), () -> {
            tipDialog.dismiss();
            onTipSure.onSure();
        });
        return tipDialog;
    }

    public static MaterialDialog customTip(Context context, String title, String content, String deal) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.custom_tip_view, false)
                .autoDismiss(true)
                .show();
        View customeView = dialog.getCustomView();
        ((TextView) customeView.findViewById(R.id.tv_title)).setText(title);
        ((TextView) customeView.findViewById(R.id.tv_content)).setText(content);
        ((TextView) customeView.findViewById(R.id.tv_deal)).setText(deal);
        ClickUtil.click((TextView) customeView.findViewById(R.id.tv_deal), () -> {
            dialog.dismiss();
        });
        return dialog;
    }

    public static AlertDialog customTip1(Context context, String title, String content, String deal, OnTipSure onTipSure) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.dialog_fabi_tip, null);
        ((TextView) view.findViewById(R.id.tv_title)).setText(title);
        ((TextView) view.findViewById(R.id.tv_desc)).setText(content);
        ((TextView) view.findViewById(R.id.tv_apply)).setText(deal);
        builder.setView(view);
        builder.setCancelable(true);
        AlertDialog tipDialog = builder.create();
        ClickUtil.click(view.findViewById(R.id.tv_cancel), () -> {
            tipDialog.dismiss();
        });
        ClickUtil.click(view.findViewById(R.id.tv_apply), () -> {
            tipDialog.dismiss();
            onTipSure.onSure();
        });
        return tipDialog;
    }


    public static MaterialDialog publishConfirmTip(Context context, boolean isbuy, String price, String amount, String money, OnTipSurePublish onTipSure) {
//        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.dialog_fabi_publish_confirm, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View view = dialog.getCustomView();
        ((ViewGroup) (view.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (view.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (view.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (view.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (view.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((TextView) view.findViewById(R.id.tv_price)).setText(price + " CNY");
        ((TextView) view.findViewById(R.id.tv_amount)).setText(amount + " USDT");
        ((TextView) view.findViewById(R.id.tv_money)).setText(money + " CNY");
        ((TextView) view.findViewById(R.id.tv_price_left)).setText(isbuy ? App.INSTANCE.getString(R.string.fabimineJavaTipUtil4) : App.INSTANCE.getString(R.string.fabimineJavaTipUtil5));
        ((TextView) view.findViewById(R.id.tv_amount_left)).setText(isbuy ? App.INSTANCE.getString(R.string.fabimineJavaTipUtil6) : App.INSTANCE.getString(R.string.fabimineJavaTipUtil7));
        ((TextView) view.findViewById(R.id.tv_money_left)).setText(isbuy ? App.INSTANCE.getString(R.string.fabimineJavaTipUtil8) : App.INSTANCE.getString(R.string.fabimineJavaTipUtil9));
        EditText et_pwd = view.findViewById(R.id.et_pwd);
        ((Button) view.findViewById(R.id.btn_confirm)).setBackgroundResource(isbuy ? R.drawable.fabi_confirm_red_bg : R.drawable.fabi_confirm_green_bg);
//        builder.setView(view);
//        builder.setCancelable(true);
//        AlertDialog tipDialog = builder.create();
//        tipDialog.getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(context, R.color.transparent));
        ClickUtil.click(view.findViewById(R.id.tv_cancel), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(view.findViewById(R.id.btn_confirm), () -> {
            if (TextUtils.isEmpty(et_pwd.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabimineJavaTipUtil10));
                return;
            }
            if (!CheckUtil.isPayPwd(et_pwd.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strBottomUpUtil201));
                return;
            }
            dialog.dismiss();
            onTipSure.onSure(et_pwd.getText().toString());
        });
        return dialog;
    }


    public interface OnTipSure {
        void onSure();
    }

    public interface OnTipSurePublish {
        void onSure(String pwd);
    }
}
